﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Splittergame
{
    class ExplosionSystem
    {

        float elapsedTime;
        float percentAnimated;
        float timeElapsed;
        float maxTime;
        int numberOfFrames;
        int numberOfFramesX;
        int frameX;
        int frameY;
        Texture2D explosion;
        Vector2 explosionPosition;

        public ExplosionSystem(Vector2 startPosition)
        {

            explosionPosition = startPosition;
            maxTime = 0.5f;
            numberOfFrames = 24;
            numberOfFramesX = 4;
        }

        internal void LoadContent(ContentManager content) 
        {
            explosion = content.Load<Texture2D>("explosion"); 
        }

        internal void Update(GameTime elapsedTimeSeconds)
        {
            elapsedTime = (float)elapsedTimeSeconds.ElapsedGameTime.TotalSeconds;
        }

        internal void Draw(SpriteBatch spriteBatch, Camera camera) 
        {

            timeElapsed += elapsedTime;
            percentAnimated = timeElapsed / maxTime;
            int frame = (int)(percentAnimated * numberOfFrames);

            frameX = frame % numberOfFramesX;
            frameY = frame / numberOfFramesX;

            //spriteWidth = explosion.Width / 4;
            //spriteHeigth = explosion.Height / 6;

            Rectangle explosionRectangle = new Rectangle(frameX * 128, frameY * 128, 128, 128);
            Rectangle test2 = camera.getVisualRect(explosionPosition.X, explosionPosition.Y, 1);

            spriteBatch.Draw(explosion, test2, explosionRectangle, Color.White, 0, new Vector2(0,0), SpriteEffects.None, 0);

            //if (timeElapsed > maxTime)
            //{
            //    timeElapsed = 0;
            
            //}

        }
    }
}
